package Presenter;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;
import Models.DiscountCode;

public class DiscountCodePresenter extends AbstractPresenter<DiscountCode>{
	final String SINGLEITEM = "discountCode";
	final String MULTIITEM = "discountCodes";
	
	public ArrayList<DiscountCode> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public DiscountCode tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public DiscountCode createItemFromJSONObject(JSONObject jsonObject){
		DiscountCode dc = new DiscountCode();
		try{
			dc.setDescription(jsonObject.getString("discName"));
			dc.setDiscNum(jsonObject.getDouble("discNum"));
			dc.setId(jsonObject.getInt("discID"));
			return dc;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(DiscountCode item){
		try{
			JSONObject json = new JSONObject();
			json.put("discName", item.getDescription());
			json.put("discNum", item.getDiscNum());
			json.put("discID", item.getId());
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public DiscountCode getEmptyEntity(){
		DiscountCode entity = new DiscountCode();
		return entity;
	}
}
